package HuffmanTrees;

/**
 * Arbol para codificacion Huffman. 
 * @author duane a. bailey
 * Modificado por Vinicio Gomez
 * @since 13/09/2012
 * @version 1.0
 * 
 */
public class HuffmanTree implements Comparable<HuffmanTree>
{
    private BinaryTree<Node> empty;
    private BinaryTree<Node> root; // root of tree
    private int totalWeight;     // weight of tree

    /**
     * Constructor de un arbol sin hojas.
     * @param e El nodo a incluir en el Arbol
     */
    public HuffmanTree(Node e)
    // post: construct a node with associated character
    {
        empty = new BinaryTree<Node>();
        root = new BinaryTree<Node>(e,empty,empty);
        totalWeight = e.getFreq();
    }

    /**
     * Constructor de un arbol con dos hojas.
     * @param left Hoja izquierda.
     * @param right Hoja derecha.
     */
    public HuffmanTree(HuffmanTree left, HuffmanTree right)
    // pre: left and right non-null
    // post: merge two trees together and merge their weights
    {
        this.empty = null;
        this.totalWeight = left.totalWeight + right.totalWeight;
        root = new BinaryTree<Node>(new Node(totalWeight),left.root,right.root);
    }

    /**
     * Raiz del arbol.
     * @return la raiz del arbol.
     */
    public BinaryTree<Node> getRoot() {
        return root;
    }

    @Override
    public int compareTo(HuffmanTree other)
    // pre: other is non-null
    // post: return integer reflecting relation between values
    {
        HuffmanTree that = (HuffmanTree)other;
        return this.totalWeight - that.totalWeight;
    }

    @Override
    public boolean equals(Object that)
    // post: return true if this and that are same tree instance
    {
        return this == that;
    }
}